<template>
  <div class="container">
    <el-button type="primary" @click="details">添加客服</el-button>
    <el-button type="primary" @click="set">设置客服</el-button>
    <div>
      <el-table :data="tableData" :empty-text="emptyText" style="width: 100%">
        <el-table-column label="名称" prop="user_name" />
        <el-table-column label="客服类型" prop="type_name" />
        <el-table-column label="状态" prop="status_name" />
        <el-table-column label="操作" prop="address">
          <template slot-scope="scoped">
            <el-button
              v-permissions="{ permission: ['add:NotifySetting'] }"
              type="danger"
              @click="deletes(scoped.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog
        :before-close="handleClose"
        title=""
        :visible.sync="dialogVisible"
        width="50%"
      >
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane
            v-for="(val, key, index) in tabs"
            :key="index"
            :label="val"
            :name="key"
          />
        </el-tabs>
        <el-table
          ref="multipleTable"
          :data="userData"
          :row-key="getRowKeys"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            :reserve-selection="true"
            :selectable="checkSelectable"
            type="selection"
            width="55"
          />

          <el-table-column label="姓名" prop="real_name" />
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="Submit">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="客服设置" :visible.sync="setDialogVisible" width="50%">
        <div>
          <span>分配方式：</span>
          <span style="margin-left: 10px">
            <el-radio-group v-model="radio">
              <el-radio label="auto">自动分配</el-radio>
              <el-radio label="manual">手动分配</el-radio>
            </el-radio-group>
          </span>
        </div>

        <div v-if="radio == 'auto'" style="margin-top: 30px">
          <span>接待模式：</span>
          <span style="margin-left: 10px">
            <el-radio-group v-model="steName">
              <el-radio label="many">单多优先</el-radio>
              <el-radio label="less">单少优先</el-radio>
              <el-radio label="polling">轮询</el-radio>
            </el-radio-group>
          </span>
        </div>

        <span slot="footer" class="dialog-footer">
          <el-button @click="setDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="steSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import {
    userServiceList,
    userServiceType,
    userServiceAdd,
    getUserServiceList,
    userServiceDel,
    userServiceSetting,
    userServiceSettingAdd,
  } from '@/api/customerService'

  export default {
    name: 'VabErrorLog',
    data() {
      return {
        dialogVisible: false,
        setDialogVisible: false,
        userData: [],
        emptyText: ' ',
        tableData: [],
        user_ids: '',
        msg_channel_id: '',
        arr: [],
        tabs: {},
        type: '',
        activeName: '',
        radio: '',
        steName: '',
      }
    },
    computed: {},
    watch: {},
    beforeMount() {},
    mounted() {
      this.init()
    },
    methods: {
      init() {
        const loading = this.$loading({
          lock: true,
          target:
            this.tableData.length == 0
              ? '.el-table__empty-block'
              : '.el-table__body',
        })
        // getUserList().then((e) => {
        //   this.userData = e.data
        // })
        getUserServiceList().then((e) => {
          this.tableData = e.data
          loading.close()
          if (e.data.length == 0) {
            this.emptyText = '暂无数据'
          }
        })

        userServiceType().then((e) => {
          console.log(e)
          this.tabs = e.data
        })
      },

      Submit() {
        const data = {
          type: this.type,
          user_ids: JSON.stringify(this.user_ids),
        }
        console.log(data)
        userServiceAdd(data).then(() => {
          this.dialogVisible = false
          this.init()
        })
      },
      steSubmit() {
        const data = {
          allot_type: this.radio,
        }
        if (this.radio == 'auto') {
          data.take_type = this.steName
        }
        userServiceSettingAdd(data).then(() => {
          this.setDialogVisible = false
          this.init()
        })
      },
      details() {
        this.userData = []
        // this.msg_channel_id = e.id
        // const arr = []
        // e.user_msg_channel.forEach((j) => {
        //   this.userData.forEach((i) => {
        //     if (j.user_id == i.id) {
        //       arr.push(i)
        //     }
        //   })
        // })
        this.activeName = ''
        this.dialogVisible = true
        // this.$nextTick(() => {
        //   this.$refs.multipleTable.clearSelection()
        //   this.toggleSelection(arr)
        // })
      },
      set() {
        userServiceSetting().then((e) => {
          this.setDialogVisible = true
          console.log(e)
          this.radio = e.data.allot_type
          this.steName = e.data.take_type
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(() => {
            done()
          })
          .catch(() => {})
      },

      toggleSelection(arr) {
        if (arr) {
          arr.forEach((row) => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange(val) {
        const arr = []
        val.forEach((j) => {
          arr.push(j.user_id)
        })
        this.user_ids = arr
      },
      handleClick(e) {
        this.type = e.name
        const data = {
          type: e.name,
        }
        userServiceList(data).then((e) => {
          console.log(e)
          this.userData = e.data
        })
      },
      getRowKeys(row) {
        return row.user_id
      },
      checkSelectable(row) {
        return row.able == 'Y'
      },
      deletes(e) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const data = {
              id: e.id,
            }
            userServiceDel(data).then(() => {
              this.init()
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      },
    },
  }
</script>

<style scoped>
  .container {
    padding: 15px;
  }
  table.tftable,
  table.table-count {
    font-size: 12px;
    color: #333333;
    width: 100%;
    border-width: 1px;
    border-collapse: collapse;
  }

  table.table-count th,
  table.table-count td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: center;
  }

  table.tftable th,
  table.tftable td {
    font-weight: 400;
    font-style: normal;
    font-size: 12px;
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #e4e4e4;
    text-align: left;
    text-align: center;
  }

  .tftable {
    margin-left: 1%;
    margin-bottom: 10px;
    margin-top: 10px;
  }
  table.tftable tr:hover {
    background: #c3ecff;
  }
  .tftable > thead > tr:hover {
    background: white;
  }
</style>
